﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniCalc
{
    public class ResultOutOfRangeException : ApplicationException
    {
    }

    public class NegativeParameterException : ApplicationException
    {
    }

    public class Calculator
    {
        public int Add(int a, int b)
        {
            int result;

            //if (a < 0 || b < 0)
            //{
            //    NegativeParameterException npEx = new NegativeParameterException();
            //    throw npEx;
            //}
            CheckForNegativeNumbers(a, b);

            result = a + b;

            if (result < 0)
            {
                ResultOutOfRangeException rangeEx = new ResultOutOfRangeException();
                throw rangeEx;
            }
            return result;
        }

        public int Subtract(int numberToSubtract, int subtractFrom)
        {
            int result;
            //if (subtractFrom < 0 || numberToSubtract < 0)
            //{
            //    NegativeParameterException npEx = new NegativeParameterException();
            //    throw npEx;
            //}
            CheckForNegativeNumbers(subtractFrom, numberToSubtract);

            result = subtractFrom - numberToSubtract;
            if (result < 0)
            {
                ResultOutOfRangeException rangeEx = new ResultOutOfRangeException();
                throw rangeEx;
            }
            return result;
        }

        public void CheckForNegativeNumbers(int a, int b)
        {
            if (a < 0 || b < 0)
            {
                NegativeParameterException npEx = new NegativeParameterException();
                throw npEx;
            }
        }

        public enum CalcOperation
        {
            Add = 0,
            Subtract = 1,
        }

        public int Calculate(int a, CalcOperation op, int b)
        {
            int nResult = 0;
            if (CalcOperation.Add == op)
            {
                nResult = Add(a, b);
            }
            else if (CalcOperation.Subtract == op)
            {
                nResult = Subtract(b, a);
            }
            return nResult;
        }
    }
}
